/**
 * SoundManager methods.
 * @author Terry Brashaw
 * @date Oct 5
 */

#include "SoundManager.h"

SoundManager::SoundManager() {
    Initialize();
}

SoundManager::~SoundManager() {
    // Use stl::vector's Clear function
    soundWrap.clear();
}

void SoundManager::Initialize() {
    // Add all the sounds that are going to be used in the game.
    // void AddSound(const string& filename,const string& identifier, bool loops);
    AddSound("assets/sfx/pick_up_stick.wav", "pick", false);
}

void SoundManager::AddSound(const string& relPath, const string& sId, bool loops) {
    soundWrap.push_back(new SoundWrap(relPath, sId, soundWrap.size(), loops));
}

/**
 * Returns the SoundBuffer item being stored so other objects that
 * may have a Sound object can set it.
 * */
SoundBuffer& SoundManager::GetSoundBuffer(const string& sId) {
    int index = FindIndex(sId);

    if (index >= 0)
        return soundWrap[index]->sndbuff;
    else
        return soundWrap[0]->sndbuff;
}

SoundBuffer& SoundManager::GetSoundBuffer(int iId) {
    if (iId < (int) soundWrap.size() && iId >= 0)
        return soundWrap[iId]->sndbuff;

    // iId is out of bounds, return default
    return soundWrap[0]->sndbuff;
}

/**
 * Plays sound file based on identifier given
 * */
void SoundManager::PlaySound(const string& sId) {
    int index = FindIndex(sId);

    if (index >= 0)
        soundWrap[index]->snd.Play();
}

void SoundManager::PlaySound(int iId) {
    if (iId < (int) soundWrap.size() && iId >= 0)
        soundWrap[iId]->snd.Play();
}

/**
 * Returns the index of the desired asset based on the string passed
 * @param sId string			string identifier of the asset
 * */
int SoundManager::FindIndex(const string& sId) {
    int index = -1;
    for (int i = 0; i < (int) soundWrap.size(); i++) {
        if (soundWrap[i]->sId == sId) {
            index = i;
            break;
        }
    }

    return index;
}
